/**
 * 
 */
package com.jack.hhitseat.service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class HttpClient {
	private final Logger logger = LoggerFactory.getLogger(HttpClient.class);
	private static HttpHeaders header = new HttpHeaders();
	private final static String YZM_URL = "http://seat.hhit.edu.cn/ClientWeb/pro/page/image.aspx";
	
	public HttpClient() {
		header.set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
		header.set("Accept-Encoding","gzip, deflate");
		header.set("Accept-Language","en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7");
		header.set("Cache-Control","no-cache");
		header.set("Connection","keep-alive");
		header.set("Host","seat.hhit.edu.cn");
		header.set("Pragma","no-cache");
		header.set("Upgrade-Insecure-Requests","1");
		header.set("User-Agent","Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; MI 2SC Build/JRO03L) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.0.3.1.355");
	}
	 /**
	 * Method name: client <BR>
	 * Description: 登录获取session <BR>
	 * @param url
	 * @param method
	 * @param params
	 * @return  String<BR>
	 */
	public String client(String url, HttpMethod method, MultiValueMap<String, Object> params) {
		
		HttpHeaders headers = new HttpHeaders();
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params,headers);
		ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class);
		
		String body = response2.getBody();

		String session ="LOGIN_ERR";//登录错误
		String result = null;
		try {
			result = body.split("<span id=\"MSG\">")[1].split("</span></div></td></tr>")[0];
			if(result.contains("无管理权限")) {			
				session =response2.getHeaders().toString().split("ASP.NET_SessionId=")[1].split("; path=/; ")[0];
			}
		}catch (Exception e) {
			session =response2.getHeaders().toString().split("ASP.NET_SessionId=")[1].split("; path=/; ")[0];
		}

		return session;
	}
	
	public String login(String url, String session) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
		header.add("Cookie","ASP.NET_SessionId="+session);
		HttpEntity<String> entity = new HttpEntity<>(header);
		
		ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, entity, String.class);

		return response.getBody();
	}
	
	public String qz(String url, String session, MultiValueMap<String, Object> params) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);

		header.set("Cookie","ASP.NET_SessionId="+session);

		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(header);
		
		ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,params);

		System.out.println(response.toString());
		return response.getBody();
	}
	
	/**
	 * 请求验证码
	 * @param session
	 */
	public void yzm(String session) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
		header.set("Cookie","ASP.NET_SessionId="+session);
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,header);

		ResponseEntity<String> response = null;
		try {
			response = template.exchange(YZM_URL, HttpMethod.GET, httpEntity, String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 请求验证码
	 * @param session
	 */
	public byte[] yzm2Api(String session) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
		header.set("Cookie","ASP.NET_SessionId="+session);
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,header);

		ResponseEntity<byte[]> response = null;
		long s = System.currentTimeMillis();
		response = template.exchange(YZM_URL, HttpMethod.GET, httpEntity, byte[].class);
		return response.getBody();
	}
	public void yzm() {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,header);

		ResponseEntity<String> response = null;
		try {
			response = template.exchange(YZM_URL, HttpMethod.GET, httpEntity, String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String qz2(String url, String session, Map<String, Object> params) {
		
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(30000);  
	    requestFactory.setReadTimeout(30000);
	    RestTemplate template = new RestTemplate(requestFactory);
	    
		  header.set("Cookie","ASP.NET_SessionId="+session);
		  HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,header);
		  
		  ResponseEntity<String> response = null;
		  
		  try { 
			  response = template.exchange(url, HttpMethod.GET, httpEntity, String.class, params); 
		  } catch (Exception e) { 
				  e.printStackTrace(); 
		  }
		  
		  return response.getBody();
	}
	
	public static ResponseEntity<String> login2wx(String url,String session, MultiValueMap<String, Object> params) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();  
		requestFactory.setConnectTimeout(10000);  
	    requestFactory.setReadTimeout(10000);

	    
	    RestTemplate templates = new RestTemplate(requestFactory);
	    
		header.set("Cookie","ASP.NET_SessionId="+session);
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params,header);
		ResponseEntity<String> response2 = templates.postForEntity(url, httpEntity, String.class);
		
		return response2;
	}
}
